package com.hotel.customersystem.service.impl;

import com.hotel.customersystem.pojo.CustomerOrder;
import com.hotel.customersystem.service.OrderRedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service("orderRedisService")
public class OrderRedisServiceImpl implements OrderRedisService {
    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public boolean saveOrder2Redis(CustomerOrder order) {
        String key ="unPayedOrder_"+order.getUserId() + "_"+order.getOrderNo();  // unPayedOrder_用户id_订单id
        redisTemplate.opsForValue().set(key,order,20, TimeUnit.SECONDS);
        return true;
    }

    @Override
    public CustomerOrder getOrderByIdFromRedis(Integer userId, String orderNo) {
        String key ="unPayedOrder_"+userId + "_"+orderNo;  // unPayedOrder_用户id_订单id

        CustomerOrder order =(CustomerOrder) redisTemplate.opsForValue().get(key);
        System.out.println("从redis中获得该订单：" + order);
        if(order !=null){
            boolean deleteResult = redisTemplate.delete(key);
            if(deleteResult)
                System.out.println("获得该订单，并从redis中移除");
        }
        return order;
    }

//    @Override
//    public boolean saveUnPayedMessage2Redis(Integer userId, String orderNo) {
//        return false;
//    }
//
//    @Override
//    public String getUnReadMessageFromRedis(Integer userId, String orderNo) {
//        return null;
//    }
}
